Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

some workload mode bugfix and waypoint optimization #361

Merged
merged 2 commits into from
May 27, 2024

Conversation

kwb0523
Copy link
Contributor

@kwb0523 kwb0523 commented May 23, 2024

1.fix potential target port modify error when access backend directly
2.fix a backend that belongs to multi services
3.store waypoint info in service to accelerate waypoint access

What type of PR is this?

What this PR does / why we need it:

Which issue(s) this PR fixes:
Fixes #

Special notes for your reviewer:

Does this PR introduce a user-facing change?:


@kwb0523 kwb0523 force-pushed the workload-dev branch 3 times, most recently from c2d6b8e to 3d897b5 Compare May 23, 2024 12:58
log.Warnf("current only supprt single ip of a pod")
}

for _, ip := range workload.GetAddresses() { // current only support signle ip, if a pod have multi ips, the last one will be stored finally
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will multiple IPs be supported later?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if support is needed in the future, but currently it's not supported.

bv.Services[bv.ServiceCount] = p.hashName.StrToNum(serviceName)
bv.ServiceCount++
if bv.ServiceCount >= bpf.MaxServiceNum {
log.Warnf("exceed the max service count, currently, a pod can belong to a maximum of 10 services")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be better to put the serviceName in warnning here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what you meant don't understand

@LiZhenCheng9527
Copy link
Collaborator

Don't use force-push if you don't have to, in case someone else is reviewing it.

Copy link
Member

@hzxuzhonghu hzxuzhonghu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kwb0523 Please update the proposal doc as well to facilitate review

)

bk.BackendUid = uid
if err = p.bpf.BackendLookup(&bk, &bv); err == nil {
if err := p.bpf.BackendLookup(&bk, &bv); err == nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you'd better log the err

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

addr := waypoint.GetAddress().Address
bv.WaypointAddr = nets.ConvertIpByteToUint32(addr)
bv.WaypointPort = nets.ConvertPortToBigEndian(waypoint.GetHboneMtlsPort())
bv.IPv4 = nets.ConvertIpByteToUint32(ip)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is the byte order now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

little endian, always like this.

LbPolicy uint32 // load balancing algorithm, currently only supports random algorithm
EndpointCount uint32 // endpoint count of current service
LbPolicy uint32 // load balancing algorithm, currently only supports random algorithm
ServicePort ServicePorts // ServicePort[i] and TargetPort[i] are a pair, i starts from 0 and max value is MaxPortNum-1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, it's better to define a PortPair data struct.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

currently,we use cilium bpf interface to operate the map, and the interface cannot support self defined data struct.


for i, port := range ports {
if i >= bpf.MaxPortNum {
log.Warnf("exceed the max port count,current only support maximum of 10 ports")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pls record detailed log information.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed.

}

sv.ServicePort[i] = nets.ConvertPortToBigEndian(port.ServicePort)
if strings.Contains(serviceName, "waypoint") {
Copy link
Contributor

@nlgwcy nlgwcy May 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe incorrect judgment if ServiceName contains waypoint, but actually it is not waypoint service. FYI, it is no need to check each time.

Copy link
Contributor Author

@kwb0523 kwb0523 May 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, there is indeed such a problem at present, and we will track the relevant optimization situation through this issue #370 in the future.


if waypoint != nil {
sv.WaypointAddr = nets.ConvertIpByteToUint32(waypoint.GetAddress().Address)
sv.WaypointPort = nets.ConvertPortToBigEndian(waypoint.GetHboneMtlsPort())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

waypoint.GetHboneMtlsPort() -> KmeshWaypointPort

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here stored are ip and port of a waypoint service, not the waypoint backend port info.

bv.IPv4 = nets.ConvertIpByteToUint32(ip)
bv.ServiceCount = 0
for serviceName := range portList {
bv.Services[bv.ServiceCount] = p.hashName.StrToNum(serviceName)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the code, some Map such as endpointsByService using serviceName(string key) for store, however, call StrToNum when map is read. If the map is directly stored by number, conversion is not required and more efficiency.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is mainly about the optimization related to endpointsByService, which may be carried out later and is not related to the current PR processing problem.

BPF_LOG(DEBUG, BACKEND, "access the backend by service:%d \n", service_id);
#pragma unroll
for (__u32 j = 0; j < MAX_PORT_COUNT; j++) {
if (service_v->service_port[j] != 0 && user_port == service_v->service_port[j]) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

service_v->service_port[j] != 0 -- no need to check

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

}
}

BPF_LOG(ERR, BACKEND, "failed to get the backend\n");
return -ENOENT;
}

// access the backend directly
static inline int direct_backend_manager(ctx_buff_t *ctx, backend_value *backend_v)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This layer of function encapsulation seems unnecessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

{
int ret;
address_t target_addr;
__u32 *sk = (__u32 *)ctx->sk;
struct bpf_sock_tuple value_tuple = {0};

if (backend_v->waypoint_addr != 0 && backend_v->waypoint_port != 0) {
BPF_LOG(DEBUG, BACKEND, "find waypoint addr=[%u:%u]\n", backend_v->waypoint_addr, backend_v->waypoint_port);
if (addr != 0 && port != 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest checking this before calling waypoint_manager, it would be more readable

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

for (unsigned int i = 0; i < backend_v->port_count; i++) {
if (i >= MAX_PORT_COUNT) {
for (__u32 i = 0; i < backend_v->service_count; i++) {
if (i >= MAX_SERVICE_COUNT) {
BPF_LOG(WARN, BACKEND, "exceed the max port count\n");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should update the log, max service count

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

BPF_LOG(DEBUG, BACKEND, "get the backend addr=[%u:%u]\n", target_addr.ipv4, target_addr.port);
return 0;
if (service_id == backend_v->service[i]) {
BPF_LOG(DEBUG, BACKEND, "access the backend by service:%d \n", service_id);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

\n is not needed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@@ -50,7 +50,7 @@ static inline int frontend_manager(ctx_buff_t *ctx, frontend_value *frontend_v)
}

if (direct_backend) {
ret = backend_manager(ctx, backend_v);
ret = direct_backend_manager(ctx, backend_v);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add some comments, this is actually pod direct access, if a pod has watpoint captured, we will redirect to waypoint here. If it is not waypoint captured, we do nothing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

1.fix potential target port modify error when access backend directly
2.fix a backend that belongs to multi services
3.store waypoint info in service to accelerate waypoint access

Signed-off-by: kwb0523 <kwb0523@163.com>
@codecov-commenter
Copy link

codecov-commenter commented May 27, 2024

Codecov Report

Attention: Patch coverage is 0% with 46 lines in your changes are missing coverage. Please review.

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Flag Coverage Δ
unittests 31.21% <0.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files Coverage Δ
pkg/controller/workload/bpfcache/backend.go 0.00% <ø> (ø)
pkg/controller/workload/bpfcache/service.go 0.00% <ø> (ø)
pkg/controller/workload/workload_processor.go 13.79% <0.00%> (ø)

BPF_LOG(WARN, BACKEND, "exceed the max port count\n");
for (__u32 i = 0; i < backend_v->service_count; i++) {
if (i >= MAX_SERVICE_COUNT) {
BPF_LOG(WARN, BACKEND, "exceed the max port count:%d\n", MAX_SERVICE_COUNT);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wrong log, it is max service number

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rm \n

DEBUG, BACKEND, "get the backend addr=[%pI4h:%u]\n", &target_addr.ipv4, bpf_ntohs(target_addr.port));
return 0;
if (service_id == backend_v->service[i]) {
BPF_LOG(DEBUG, BACKEND, "access the backend by service:%d\n", service_id);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rm \n

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that all logs have been added with ‘\n’. Do we need to delete them all together in this PR.

BPF_LOG(
DEBUG,
BACKEND,
"get the backend addr=[%pI4h:%u]\n",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rm \n

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

BPF_LOG(
DEBUG,
FRONTEND,
"find waypoint addr=[%pI4h:%u]\n",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rm \n

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

BPF_LOG(
DEBUG,
SERVICE,
"find waypoint addr=[%pI4h:%u]\n",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rm \n

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Signed-off-by: kwb0523 <kwb0523@163.com>
Copy link
Member

@hzxuzhonghu hzxuzhonghu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/lgtm

@kmesh-bot
Copy link
Collaborator

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: hzxuzhonghu

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@kmesh-bot kmesh-bot merged commit 03bb5a4 into kmesh-net:main May 27, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants